package cayte.demo.dict;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class RememberActivity extends Activity implements OnClickListener {
	public static final int NORMAL = 0;
	public static final int ONLY_EN = 1;
	public static final int ONLY_CN = 2;
	private ViewPager mPager;
	private List<View> listViews;
	private ArrayList<Word> words;
	private LayoutInflater mInflater;
	private int MODE;
	private XfTTSHandler tts = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.remember);

		tts = new XfTTSHandler(this);
		int initType = tts.initTTS();
		switch (initType) {
		case XfTTSHandler.SUCCESS:
			break;
		case XfTTSHandler.ERROR_NO_TTS:
		default:
			Toast.makeText(this, getString(R.string.noTTSTip),
					Toast.LENGTH_SHORT).show();
			break;
		}
		InitData();
		InitViews();
		InitViewPager();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		tts.end();
		tts = null;
	}

	private void InitData() {
		words = new ArrayList<Word>();
		MODE = getIntent().getIntExtra("mode", NORMAL);
		int index = getIntent().getIntExtra("index", -1);
		if (index == -1)
			words = DictData.instance().getAll();
		else
			words = DictData.instance().get(index);
	}

	private void InitViews() {
		listViews = new ArrayList<View>();
		mInflater = getLayoutInflater();
		for (Word word : words) {
			listViews.add(getView(word));
		}
	}

	private View getView(Word word) {
		View view = mInflater.inflate(R.layout.view, null);
		TextView en = (TextView) view.findViewById(R.id.en);
		TextView cn = (TextView) view.findViewById(R.id.cn);
		en.setText(word.getEn());
		cn.setText(word.getCn());
		switch (MODE) {
		case NORMAL:
			en.setVisibility(View.VISIBLE);
			cn.setVisibility(View.VISIBLE);
			break;
		case ONLY_EN:
			en.setVisibility(View.VISIBLE);
			cn.setVisibility(View.INVISIBLE);
			break;
		case ONLY_CN:
			en.setVisibility(View.INVISIBLE);
			cn.setVisibility(View.VISIBLE);
			break;
		}
		if (tts.isEnable) {
			final String strEn = word.getEn();
			en.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tts.speak(strEn);
				}
			});
			final String strCn = word.getCn();
			cn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					tts.speak(strCn);
				}
			});
		}
		view.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		v.findViewById(R.id.en).setVisibility(View.VISIBLE);
		v.findViewById(R.id.cn).setVisibility(View.VISIBLE);
	}

	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);
	}

	public class MyPagerAdapter extends PagerAdapter {
		public List<View> mListViews;

		public MyPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
			overridePendingTransition(0, 0);
		}
		return super.onKeyDown(keyCode, event);
	}

}
